package com.ezandroid.library.network.http.params;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Http params.
 *
 * @author markmjw
 * @date 2014-04-25
 */
public class HttpParams implements Serializable {
    private static final long serialVersionUID = -6376078803512393464L;

    protected HashMap<String, String> mTextParams = new HashMap<>();
    protected HashMap<String, File> mFileParams = new HashMap<>();

    public HttpParams() {

    }

    public void put(String key, String value) {
        if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
            mTextParams.put(key, value);
        }
    }

    public void put(String key, Object value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, File file) {
        if (!TextUtils.isEmpty(key) && null != file && file.exists()) {
            mFileParams.put(key, file);
        }
    }

    public void putTexts(Map<String, String> source) {
        if (source != null && !source.isEmpty()) {
            for (Map.Entry<String, String> entry : source.entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
        }
    }

    public void putFiles(Map<String, File> source) {
        if (source != null && !source.isEmpty()) {
            for (Map.Entry<String, File> entry : source.entrySet()) {
                put(entry.getKey(), entry.getValue());
            }
        }
    }

    public void remove(String key) {
        mTextParams.remove(key);
        mFileParams.remove(key);
    }

    public boolean hasMultipartParams() {
        return !mFileParams.isEmpty();
    }

    public boolean hasTextParams() {
        return !mTextParams.isEmpty();
    }

    public HashMap<String, String> getTextParams() {
        return mTextParams;
    }

    public HashMap<String, File> getFileParams() {
        return mFileParams;
    }

    public SimpleMultipartEntity getMultipart() throws IOException {
        if (mFileParams.isEmpty()) {
            return null;
        }

        final SimpleMultipartEntity multipart = new SimpleMultipartEntity();
        // Add string params
        multipart.addTextParts(mTextParams);
        // Add file params
        multipart.addFileParts(mFileParams);
        // Add end boundary
        multipart.addEndBoundary();
        return multipart;
    }

    @Override
    public String toString() {
        final StringBuilder result = new StringBuilder();

        if (!mTextParams.isEmpty()) {
            result.append("\nText params: ");
            String newline = "";

            for (final HashMap.Entry<String, String> entry : mTextParams.entrySet()) {
                result.append(newline);
                result.append(entry.getKey());
                result.append("=");
                result.append(entry.getValue());

                newline = "&";
            }
        }

        if (!mFileParams.isEmpty()) {
            result.append("\nFile params: ");
            String newline = "";

            for (final HashMap.Entry<String, File> entry : mFileParams.entrySet()) {
                result.append(newline);
                result.append(entry.getKey());
                result.append("=");
                result.append(entry.getValue().getPath());

                newline = "&";
            }
        }

        return result.toString();
    }
}